package com.interauto.service;

import com.interauto.Dao.entity.Runtasktable;
import com.interauto.bean.AutoAssertInfoBean;
import com.interauto.bean.AutoCaseBean;
import com.interauto.bean.AutoInterCaseBean;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.bean.httpinfo.HttpResPonseBean;
import com.interauto.service.alarm.AutoAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 运行用例相关的哈
 */
@Component
public class AutoRunCaseinfoService {
    @Autowired
    private AutoRunInterfaceService autoRunInterfaceService;
    @Autowired
    private AutoCaseBeanService autoCaseBeanService;
    @Autowired
    private AutoAlarmService autoAlarmService;
    @Autowired
    private AutoRunResultService autoRunResultService;

    private static final Logger logger= LoggerFactory.getLogger(AutoRunCaseinfoService.class);


    /**
     * 运行要跑的用例的入口
     * @param autoCaseBean 要运行的用例信息哈
     */
    public void runcaseinfo(AutoCaseBean autoCaseBean,Runtasktable runtasktable){
        if(autoCaseBean == null){
            logger.info("--->>runcaseinfo is fail;autoCaseBean is null");
            return;
        }
        logger.info(String.format("--->>>开始运行时间:%s;运行case信息:%s",System.currentTimeMillis(),autoCaseBean.toString()));

        List<AutoInterCaseBean> autoInterCaseBeans=autoCaseBean.getAutoInterCaseBeans();
        int caseruntid=autoRunResultService.addcaserunt(autoCaseBean,runtasktable);  //暂时没有任务，任务ID固定写死100;
        if(caseruntid < 0){
            logger.error("addcaserunt is fail;");
            return;
        }
        if(autoInterCaseBeans == null || autoInterCaseBeans.isEmpty()){
            logger.info("--->>runcaseinfo is fail;autoInterCaseBeans is empty");
            return;
        }
        boolean castrunstatus=true;
        for(AutoInterCaseBean autointerinfo:autoInterCaseBeans){
            HttpResPonseBean httpResPonseBean=new HttpResPonseBean();
            AutoAssertInfoBean autoassert = runinterinfo(autointerinfo,httpResPonseBean);
            if(autoassert.getCodetype()!=0){
                castrunstatus=false;
            }
            int interruntid=autoRunResultService.addinterrunt(autointerinfo,httpResPonseBean,
                    autoassert.getErrormessage(),caseruntid,autoassert.getCodetype());

            autoAlarmService.goalarm(autointerinfo,runtasktable,autoassert,interruntid);  //发送报警相关的
        }
        int castrunstatuscode=castrunstatus?1:2;
        autoRunResultService.udcaserunt(caseruntid,castrunstatuscode);
        logger.info(String.format("--->>>运行结束时间:%s",System.currentTimeMillis()));
    }


    public AutoAssertInfoBean runinterinfo(AutoInterCaseBean autoInterCaseBean,
                                            HttpResPonseBean httpResPonseBean){
        if(autoInterCaseBean == null){
            return new AutoAssertInfoBean(3,"运行的接口对象为空");
        }
        HttpRequestBean httpRequestBean=autoInterCaseBean.getHttpRequestBean();
        if(httpRequestBean == null){
            return new AutoAssertInfoBean(3,"运行的http请求对象为空");
        }
        HttpResPonseBean resPonseBean=autoRunInterfaceService.runifcase(httpRequestBean);
        if(resPonseBean == null){
            httpResPonseBean=null;
            return new AutoAssertInfoBean(3,"运行的http响应结果为空");
        }
        BeanUtils.copyProperties(resPonseBean,httpResPonseBean); //主要是这个运行结果需要抛出去，存库
        return autoRunInterfaceService.runcheckinfo(resPonseBean,autoInterCaseBean.getAutoAssInfoBeans());
    }



}
